package com.lookme.lmtool.cracker.filter.pixellate;

import com.jhlabs.image.BlockFilter;
import com.lookme.lmtool.cracker.CrackerConstants;
import com.lookme.lmtool.cracker.annotation.FilterAnnotation;
import com.lookme.lmtool.cracker.annotation.ParamAnnotation;
import com.lookme.lmtool.cracker.filter.AbstractFilter;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;

@FilterAnnotation(groupName = CrackerConstants.FILTER_GROUP_NAME_PIXELLATE,name="象素风格化")
public class FilterPixel extends AbstractFilter {
    @XStreamAsAttribute
    @ParamAnnotation(name = "间距", desc = "间距",inputType = ParamAnnotation.InputType.Slider,width = 300,amountToStepBy = 1,min=2,max=100,blockIncrement = 5,majorTickUnit=10,minorTickCount = 2)
    int spacing=5;

    @Override
    public BufferedImageOp getFilter() {
        return null;
    }

    @Override
    public BufferedImage filter(BufferedImage src, BufferedImage dst) {
        int width = src.getWidth();
        int height = src.getHeight();
        int minX = src.getMinX();
        int minY = src.getMinY();
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        for (int i = minX; i < width; i = i + spacing) {
            for (int j = minY; j < height; j = j + spacing) {
                int pixelRGB = src.getRGB(i, j);
                int red = (pixelRGB >> 16) & 0xff;
                int green = (pixelRGB >> 8) & 0xff;
                int blue = (pixelRGB >> 0) & 0xff;
                graphics.setColor(new Color(red, green, blue));//设置画笔的颜色
                //graphics.fillRect(i,j,k,k);//填充矩形
                graphics.fillRect(i, j, spacing, spacing);//填充圆形矩形
            }
        }
        return img;
    }
}
